package com.cloud.salon.client.authority.controller;

import com.cloud.salon.client.authority.service.UserRoleService;
import com.cloud.salon.client.authority.service.UserService;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.base.entity.authority.User;
import com.cloud.salon.common.base.vo.RelationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 用户管理控制器
 * @Author wolf--LZH
 * @CreateDate 2019/4/10 0010 下午 1:30
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户相关接口")
public class UserController {

    private final String name = "用户";

    @Autowired
    private UserService service;
    @Autowired
    private UserRoleService userRoleService;

    @PostMapping("setUserRoles")
    @ApiOperation(value="设置用户的角色")
    public Res<User> setUserRoles(@RequestBody RelationVO relationVO) {
        return userRoleService.setUserRoles(relationVO.getBaseId(), relationVO.getRelationIdList());
    }

    @PostMapping("findById")
    @ApiOperation(value="通过id查找一个" + name)
    public Res<User> findById(@RequestBody Integer id) {
        return service.findById(id);
    }

    @PostMapping("findByUsername")
    @ApiOperation(value="通过用户名获取对应的" + name)
    public Res<User> findByUsername(@RequestBody String username) {
        return service.findByUsername(username);
    }

    @PostMapping("findAll")
    @ApiOperation(value="获取所有的" + name)
    public Res<List<User>> findAll() {
        return service.findAll();
    }

    @PostMapping("save")
    @ApiOperation(value="添加一个" + name)
    public Res<User> save(@RequestBody User user) {
        return service.save(user);
    }

    @PostMapping("saveAll")
    @ApiOperation(value="批量添加" + name)
    public Res<List<User>> saveAll(@RequestBody List<User> users) {
        return service.saveAll(users);
    }

    @PostMapping("update")
    @ApiOperation(value="修改" + name + "数据")
    public Res<User> update(@RequestBody User user) {
        return service.update(user);
    }

    @PostMapping("updateAll")
    @ApiOperation(value="批量修改" + name + "数据")
    public Res<List<User>> updateAll(@RequestBody List<User> users) {
        return service.updateAll(users);
    }

    @PostMapping("deleteById")
    @ApiOperation(value="通过id删除对应" + name)
    public Res<Boolean> deleteById(@RequestBody Integer id) {
        return  service.deleteById(id);
    }

    @PostMapping("deleteByIdAll")
    @ApiOperation(value="通过id集合批量删除" + name)
    public Res<Integer> deleteByIdAll(@RequestBody List<Integer> ids) {
        return service.deleteByIdAll(ids);
    }
}
